package com.daxidi.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.daxidi.cn.entity.Bill;
import com.daxidi.cn.entity.Member;
import com.daxidi.cn.entity.User;
import com.daxidi.cn.mapper.BillMapper;
import com.daxidi.cn.mapper.MemberMapper;
import com.daxidi.cn.mapper.UserMapper;
import com.daxidi.cn.service.IMemberService;
import com.daxidi.cn.vo.MemberBillVo;
import com.daxidi.cn.vo.MemberVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addMember(Member member) {
        memberMapper.insert(member);
    }

    @Override
    public void deleteMemberById(Long id) {
        QueryWrapper<Bill> wrapperBill = new QueryWrapper<>();
        wrapperBill.eq("membe_id", id);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("membe_id", id);
        List<Bill> billList = billMapper.selectList(wrapperBill);
        if (billList.size() > 0) {
            billList.forEach(bill -> {
                billMapper.deleteById(bill.getId());
            });
        }
        List<User> users = userMapper.selectList(userQueryWrapper);
        if (users.size() > 0) {
            users.forEach(user -> {
                userMapper.deleteById(user.getId());
            });
        }
        memberMapper.deleteById(id);
    }

    @Override
    public void updateMemberById(Member member) {
        memberMapper.updateById(member);
    }

    @Override
    public Map selectMembersPage(Integer pageNum, Integer pageSize,String enable) {
        Page<Member> page = new Page<Member>(pageNum, pageSize);
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("enable",enable);
        Page<Member> memberPage = memberMapper.selectPage(page, wrapper);
        // 数据
        List<Member> records = memberPage.getRecords();
        // 总条数
        long total = memberPage.getTotal();
        Map map = new HashMap();
        map.put("count", total);
        map.put("data", records);
        return map;
    }

    @Override
    public Map selectMembersPage(String name, String mobile, String idCard, Integer enable, Integer page, Integer limit) {
        QueryWrapper<Member> wrapper = new QueryWrapper();
        if (null != name && !"".equals(name)) {
            wrapper.like("name", name);
        }
        if (mobile != null && !"".equals(mobile)) {
            wrapper.like("mobile", mobile);
        }
        if (idCard != null && !"".equals(idCard)) {
            wrapper.like("id_card", idCard);
        }
        if (enable != null && !"".equals(enable)) {
            wrapper.eq("enable", enable);
        }
        Page<Member> page2 = new Page<Member>(page, limit);
        Page<Member> memberPage = memberMapper.selectPage(page2, wrapper);
        // 数据
        List<Member> records = memberPage.getRecords();
        // 总条数
        long total = memberPage.getTotal();
        Map map = new HashMap();
        map.put("count", total);
        map.put("data", records);
        return map;
    }

    @Override
    public Member selectMemberById(Long id) {

        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("id", id);
        Member member = memberMapper.selectOne(memberQueryWrapper);
        return member;
    }

    @Override
    public Member selectMember(Long id) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("id", id);
        Member member = memberMapper.selectOne(memberQueryWrapper);
        return member;
    }

    @Override
    public Member selectMemberByMemberId(Long memberId) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("id", memberId);
        Member member = memberMapper.selectOne(memberQueryWrapper);
        return member;
    }

    @Override
    public Member selectMemberByCode(String code) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("code", code);
        Member member = memberMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Member loginMember(String mobile, String password) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        wrapper.eq("password", password);
        Member member = memberMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public List<MemberBillVo> consumption(Long memberId) {
        List<MemberVo> members = memberMapper.consumption(memberId);
        List<MemberBillVo> memberBillVos = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        for (MemberVo m : members) {
            m.setSave(m.getTotal() - m.getDiscounted());
        }
        members.forEach(memberVo -> {
            MemberBillVo memberBillVo = new MemberBillVo();
            BeanUtils.copyProperties(memberVo, memberBillVo);
            memberBillVo.setCreatedAt(memberVo.getCreatedAt());
            memberBillVo.setFree(memberVo.getSave());
            memberBillVos.add(memberBillVo);
        });
        memberBillVos.forEach(memberBillVo -> {
            memberBillVo.setMembeId(memberId);
        });
        return memberBillVos;
    }
}
